import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:wallet_utils/report_widget/base/callback/base_callback.dart';
import 'package:wallet_utils/toast.dart';
import 'package:wallet_utils/webview_common.dart';
import '../../agreement_dialog.dart';
import '../../login_page/constant.dart';
import '../../mine/api/mine_api.dart';
import '../../mine/bean/aboutUsBean.dart';
import '../../mine/contentType.dart';
import '../api/home_api.dart';
import '../bean/auditResultBean.dart';
import '../bean/popUpsBean.dart';
import '../bean/userAgreementBean.dart';
import 'state.dart';

class AuditRejectionLogic extends GetxController {
  final AuditRejectionState state = AuditRejectionState();
  List<ProductList>? productList = [];
  List<PopUpsInfo> popUpsInfo=[];
  @override
  void onInit() {
    super.onInit();
    obtainAuditResultInfo();
    getCpaPopList();
    getUser2WhereApi();
  }

  //获取用户审核结果
  void obtainAuditResultInfo() async {
    HomeApi.obtainAuditResultInfo(BaseCallback(onSuccess: (map) {
      AuditResultBean auditResultBean = AuditResultBean.fromJson(map);
      if (auditResultBean.data != null) {
        if(auditResultBean.data?.productList!=null){
          productList = auditResultBean.data?.productList;
          if (productList!.isNotEmpty || productList!=null) {
            state.loanAmount = productList![0].loanAmount;
            state.loanMinRate = productList![0].loanMinRate;
            state.loanPeriod = productList![0].loanPeriod;
            state.linkUrl = productList![0].linkUrl;
            state.productId = productList![0].productId;
            state.productName = productList![0].productName;
          }
        }
      }
      state.refreshController.refreshCompleted();
      update();
    }, onError: (msg) {
      Future.delayed(const Duration(milliseconds: 500), () {
        state.refreshController.refreshCompleted();
        ToastUtils.showToast(msg ?? "");
      });
    }));
  }

  //弹窗
  void getCpaPopList() async {
    HomeApi.getCpaPopList(
        PopUps().audit,
        BaseCallback(
            onSuccess: (map) {
          PopUpsBean popUpsBean = PopUpsBean.fromJson(map);
          popUpsInfo = popUpsBean.data!;
          if (popUpsInfo.isNotEmpty) {
            state.amount = popUpsInfo[0].amount;
            state.name = popUpsInfo[0].name;
            state.popLinkUrl = popUpsInfo[0].linkUrl;
            state.id = popUpsInfo[0].id;
          }
        },
            onError: (msg) {
              Future.delayed(const Duration(milliseconds: 500), () {
                ToastUtils.showToast(msg ?? "");
              });
            }));
  }

  //埋点
  void addLoanRecord(num productId, int showPage) async {
    HomeApi.addLoanRecord(
        productId,
        showPage,
        BaseCallback(
            onSuccess: (map) {},
            onError: (msg) {
              Future.delayed(const Duration(milliseconds: 500), () {
                ToastUtils.showToast(msg ?? "");
              });
            }));
  }

  //信用授权书
  getUserInfo(int type) {
    MineApi.getHTML(
        type,
        BaseCallback(onSuccess: (map) {
          AboutUsBean aboutUsBean = AboutUsBean.fromJson(map);
          if (aboutUsBean.data != null) {
            state.agreementName = aboutUsBean.data?.name;
            state.agreementContent = aboutUsBean.data?.content;

          }
        }, onError: (msg) {
          Future.delayed(const Duration(milliseconds: 500), () {
            ToastUtils.showToast(msg ?? "");
          });
        }));
  }

  void getUser2WhereApi() async {
    HomeApi.getUser2WhereApi(BaseCallback(onSuccess: (map) {
      UserAgreementBean userAgreementBean = UserAgreementBean.fromJson(map);
      if(userAgreementBean.data!=null){
        state.agreementContent= userAgreementBean.data?.content;
        update();
      }
    }, onError: (msg) {
      Future.delayed(const Duration(milliseconds: 500), () {
        ToastUtils.showToast(msg ?? "");
      });
    }));
  }

  void popover(int type,int index) {
    Future.delayed(Duration.zero, () {
      showDialog<Null>(
        context: Get.context!,
        barrierDismissible: false,
        useSafeArea: false,
        useRootNavigator: false,
        builder: (BuildContext context) {
          return AgreementDialog(
              name: '',
              content: state.agreementContent,
              submit: '我已阅读并同意授权',
              isClose: true,
              onTap: () {
                Get.back();
                if(type==1){
                  if (state.productId != 0) {
                    addLoanRecord(state.productId!, ShowPage.result);
                  }
                  Get.to(WebViewCommon(
                    state.linkUrl ?? '',
                  ));
                }else if(type==2){
                  if(productList!.isNotEmpty){
                    addLoanRecord(productList![index].productId!, ShowPage.result);
                  }
                  Get.to(WebViewCommon(
                    productList![index].linkUrl ?? '',
                  ));
                }else if(type==3){
                  if (state.id != 0) {
                    addLoanRecord(state.id!, ShowPage.result);
                  }
                  Get.to(WebViewCommon(
                    state.popLinkUrl ?? '',
                  ));
                }
              });
        },
      );
    });
  }
}
